#include <QMessageBox>
#include <QFileInfo>
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    this->ui->outline_tree_widget->setHeaderHidden(true);
    this->Tree.clear();
    this->current_type = -1;
    this->current_id = -1;

    this->input = new Input;
    this->input->main = (QWidget*)this;

    QFileInfo db_file("textbook.db");
    if (!db_file.isFile()) {
        QMessageBox::critical(this, "系统错误", "找不到数据库文件");
        exit(0);
        return;
    }

    this->db = QSqlDatabase::addDatabase("QSQLITE");
    this->db.setDatabaseName("textbook.db");
    if (!this->db.open()) {
        QString err_str = this->db.lastError().databaseText();
        QMessageBox::critical(this, "系统错误", "打不开数据库文件，错误信息：" + err_str);
        exit(0);
        return;
    }

    QSqlQuery q = this->db.exec("SELECT `id`, `article_name` FROM `article` ORDER BY `id` ASC");
    T t;
    t.type = 0;
    while (q.next()) {
        QString article_id = q.value("id").toString();
        QString name = q.value("article_name").toString();
        QTreeWidgetItem *item = new QTreeWidgetItem(this->ui->outline_tree_widget);
        item->setText(0, name);
        t.item = item;
        t.id = q.value("id").toInt();
        t.line = 0;
        t.title = name;
        this->Tree.append(t);
        ++t.line;
        t.type = 1;
        this->get_children_record(t, item, article_id, "-1");
    }

    this->db.close();
    QSqlDatabase::removeDatabase(this->db.databaseName());
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::get_children_record(T t, QTreeWidgetItem *item, QString article_id, QString parent_id) {
    QSqlQuery q = this->db.exec("SELECT `id`, `node_name` FROM `node` WHERE `article_id` = " +
                                article_id + " AND `parent_node_id` = " + parent_id + " ORDER BY `id` ASC");
    while (q.next()) {
        QString p_id = q.value("id").toString();
        QString name = q.value("node_name").toString();
        QTreeWidgetItem *i = new QTreeWidgetItem;
        i->setText(0, name);
        item->addChild(i);
        t.title = name;
        t.item = i;
        t.id = q.value("id").toInt();
        this->Tree.append(t);
        ++t.line;
        this->get_children_record(t, i, article_id, p_id);
    }
}

void MainWindow::on_close_clicked()
{
    this->close();
}

void MainWindow::on_select_clicked()
{
    if (this->current_type == -1 || this->current_id == -1) {
        QMessageBox::warning(this, "系统警告", "未选择任何项目");
        return;
    }
    this->input->current_type = this->current_type;
    this->input->current_id = this->current_id;
    this->input->show();
    this->hide();
}

void MainWindow::on_outline_tree_widget_itemClicked(QTreeWidgetItem *item, int column)
{
    Q_UNUSED(column);
    for (T t : this->Tree) {
        if (t.item == item) {
            this->current_type = t.type;
            this->current_id = t.id;
            return;
        }
    }
}
